package com.itboye.ihomebank.db;

import java.util.ArrayList;
import java.util.List;

import com.itboye.ihomebank.bean.BeiFenKeyBean;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 备份钥匙
 *
 */
public class KeyService {
	DBOpenHelper dbOpenHelper;
	SQLiteDatabase db;
	
	public KeyService(Context context){  
        this.dbOpenHelper=new DBOpenHelper(context);  
    } 

	/**
	 * 添加
	 * @param key
	 */
    public void add(BeiFenKeyBean key){  
        SQLiteDatabase db=dbOpenHelper.getWritableDatabase();  
        db.execSQL("insert into key(lock_id,key_id,lock_mac,lock_alias) values(?,?,?,?)",new Object[]{key.getLockId(),key.getKeyId(),key.getLockMac(),key.getLockAlilas()});  
    }
    /**
     * 删除  
     * @param id
     */
    public void delete(String id){  
        SQLiteDatabase db=dbOpenHelper.getWritableDatabase();  
        db.execSQL("delete from key where id=?",new String[]{id});  
    } 
    /**
     * 修改  
     * @param key
     */
    public void update(BeiFenKeyBean key){  
        SQLiteDatabase db=dbOpenHelper.getWritableDatabase();  
        db.execSQL("update key set lock_id=? ,key_id=?,lock_mac=?,lock_alias=? where id=?",new Object[]{key.getLockId(),key.getKeyId(),key.getLockMac(),key.getLockAlilas(),key.getId()});  
          
    } 
    /**
     * 查询  
     * @param id
     * @return
     */
    public BeiFenKeyBean find(String id){  
    	BeiFenKeyBean key=new BeiFenKeyBean();
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();  
        Cursor cursor=db.rawQuery("select * from key where id=?", new String[]{id}); 
        if(cursor.moveToFirst()){  
        	String userid=cursor.getString(cursor.getColumnIndex("id"));  
            String lockId=cursor.getString(cursor.getColumnIndex("lock_id"));  
            String keyId=cursor.getString(cursor.getColumnIndex("key_id"));  
            String lockMac=cursor.getString(cursor.getColumnIndex("lock_mac"));  
            String lockAlias=cursor.getString(cursor.getColumnIndex("lock_alias"));  
            key.setId(userid);
            key.setLockId(lockId);
            key.setKeyId(keyId);
            key.setLockMac(lockMac);
            key.setLockAlilas(lockAlias);
        } 
        return key;
    }
    /**
     * 通过锁别名查询数据  
     * @param id
     * @return
     */
    public BeiFenKeyBean aliasQuery(String lock_alias){  
    	BeiFenKeyBean key=new BeiFenKeyBean();
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();  
        Cursor cursor=db.rawQuery("select * from key where lock_alias=?", new String[]{lock_alias}); 
        if(cursor.moveToFirst()){  
        	String userid=cursor.getString(cursor.getColumnIndex("id"));  
            String lockId=cursor.getString(cursor.getColumnIndex("lock_id"));  
            String keyId=cursor.getString(cursor.getColumnIndex("key_id"));  
            String lockMac=cursor.getString(cursor.getColumnIndex("lock_mac"));  
            String lockAlias=cursor.getString(cursor.getColumnIndex("lock_alias"));  
            key.setId(userid);
            key.setLockId(lockId);
            key.setKeyId(keyId);
            key.setLockMac(lockMac);
            key.setLockAlilas(lockAlias);
        } 
        return key;
    }
    /**
     * 查找全部数据 
     * @return
     */
    public List<BeiFenKeyBean> getAllUserList(){
        List<BeiFenKeyBean> keys=new ArrayList<BeiFenKeyBean>();  
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();  
        Cursor cursor = db.rawQuery("select * from key", null);
        while (cursor.moveToNext()) {
        	String id=cursor.getString(cursor.getColumnIndex("id"));  
            String lockId=cursor.getString(cursor.getColumnIndex("lock_id"));  
            String keyId=cursor.getString(cursor.getColumnIndex("key_id"));  
            String lockMac=cursor.getString(cursor.getColumnIndex("lock_mac"));  
            String lockAlias=cursor.getString(cursor.getColumnIndex("lock_alias")); 
            BeiFenKeyBean key=new BeiFenKeyBean();  
            key.setId(id);  
            key.setLockId(lockId);
            key.setKeyId(keyId);
            key.setLockMac(lockMac);
            key.setLockAlilas(lockAlias);  
            keys.add(key);
        }
        return keys;  
    } 
    /** 
     * 分页查找数据 
     * @param offset 跳过多少条数据 
     * @param maxResult 每页多少条数据 
     * @return  
     */  
    public List<BeiFenKeyBean> getUserList(int offset, int maxResult){
        List<BeiFenKeyBean> keys=new ArrayList<BeiFenKeyBean>();  
        SQLiteDatabase db=dbOpenHelper.getReadableDatabase();  
        Cursor cursor =db.rawQuery("select * from key order by uid asc limit ? offset ?", new String[]{String.valueOf(maxResult), String.valueOf(offset)});  
        while(cursor.moveToNext()){
            String id=cursor.getString(cursor.getColumnIndex("id"));  
            String lockId=cursor.getString(cursor.getColumnIndex("lock_id"));  
            String keyId=cursor.getString(cursor.getColumnIndex("key_id"));  
            String lockMac=cursor.getString(cursor.getColumnIndex("lock_mac"));  
            String lockAlias=cursor.getString(cursor.getColumnIndex("lock_alias"));  
            BeiFenKeyBean key=new BeiFenKeyBean();  
            key.setId(id);  
            key.setLockId(lockId);
            key.setKeyId(keyId);
            key.setLockMac(lockMac);
            key.setLockAlilas(lockAlias);  
            keys.add(key);  
        }  
        return keys;  
    } 
    /**
	 * 清空数据
	 */
	public void deleteData() {
		db = dbOpenHelper.getWritableDatabase();
		db.execSQL("delete from key");
		db.close();
	}
}
